using Godot;

public partial class Bullet : Area2D
{
    [Export] public int Speed { get; set; } = 500; // 新增移动速度属性
    public Vector2 StartPosition { get; set; }
    public Node2D Target { get; set; }
    public int Damage { get; set; }

    public override void _Ready()
    {
        base._Ready();
        this.Position = StartPosition;
    }

    public override void _Process(double delta)
    {
        if (Target == null || !IsInstanceValid(Target))
        {
            QueueFree();
            return;
        }

        // 朝向目标移动
        GlobalPosition = GlobalPosition.MoveToward(Target.GlobalPosition, Speed * (float)delta);
        
        // 到达目标位置时应用伤害
        if (GlobalPosition.DistanceTo(Target.GlobalPosition) < 5)
        {
            ApplyDamageToTarget();
            QueueFree();
        }
    }

    private void ApplyDamageToTarget()
    {
        if (Target is Enemy enemy)
        {
            enemy.ApplyDamage(Damage);
        }
    }
}
